package com.atguigu.apitest.transform;

import com.atguigu.apitest.beans.SensorReading;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class TransformTest6_Partition {
    public static void main(String[] args) throws Exception{
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(4);

        DataStream<String> inputStream = env.readTextFile("src/main/resources/sensor.txt");

        inputStream.print("input");

        // 1. shuffle  随机打乱
        DataStream<String> shuffleStream = inputStream.shuffle();
//        shuffleStream.print("shuffle");

        // 2. keyBy,  相同key一定在同一个分区上
        // 转换成SensorReading类型
        DataStream<SensorReading> dataStream = inputStream.map(line -> {
            String[] fields = line.split(",");
            return new SensorReading(fields[0], new Long(fields[1]), new Double(fields[2]));
        });
//        dataStream.keyBy(data->data.getId()).print("keyBy");

        // 3. global 全都放到下游的第一个分区
        dataStream.global().print("global");

        env.execute();
    }
}
